package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.LsUser;
import com.example.xpvideo.entity.PageResult;
import com.example.xpvideo.entity.ResponseResult;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.req.UserListReq;
import com.example.xpvideo.entity.res.PhpUserRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LsUserMapper;
import com.example.xpvideo.service.LsUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * (LsUser)Service层
 *
 * @author 杨丽辉
 * @since 2021-10-18 09:52:57
 */
@Service
 @Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class LsUserServiceImpl implements LsUserService {
        @Autowired
        private LsUserMapper lsUserMapper;


    @Override
    public ResponseResult<PageResult<List<PhpUserRes>>> list(UserListReq userListReq) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();
        String account = userListReq.getAccount();
        String nickname = userListReq.getNickname();
        String superAccount = userListReq.getFirstLeader();
        String indirectSuperAccount = userListReq.getSecondLeader();
        Integer stage = userListReq.getStage();
        String phone = userListReq.getPhone();

        PageHelper.startPage(page, size);

        List<PhpUserRes> list = lsUserMapper.UserList(Wrappers.<LsUser>query()
                .eq(!ObjectUtils.isEmpty(account),"account",account)
                .like(!ObjectUtils.isEmpty(nickname),"nickname",nickname)
               /* .inSql(!ObjectUtils.isEmpty(superAccount),superAccount,)*/
                        .apply(!ObjectUtils.isEmpty(superAccount),"select (select account from ls_user l1 where l1.id = ls_user.first_leader and account = '" + superAccount + "') as firstAccount from ls_user ")
                .inSql(!ObjectUtils.isEmpty(indirectSuperAccount), "role_id", "select id from role where id = #{superAccount}")
                .eq(!ObjectUtils.isEmpty(phone),"mobile",phone)
                .eq(!ObjectUtils.isEmpty(stage),"disable",stage));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<PhpUserRes>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<PageResult<List<LsUser>>> SubordinateList(String account) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();

        PageHelper.startPage(page, size);

        List<LsUser> list = lsUserMapper.selectList(Wrappers.<LsUser>query().select("nickname","avatar","mobile","sex",
                        "account","distribution_code","create_time")
                .eq(!ObjectUtils.isEmpty(account), "first_leader",account));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<LsUser>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<PageResult<List<LsUser>>> InSubordinateList(String account) throws ErrorException {

        int page = PageContext.getPage();
        int size = PageContext.getSize();

        PageHelper.startPage(page, size);

        List<LsUser> list = lsUserMapper.selectList(Wrappers.<LsUser>query().select("nickname","avatar","mobile","sex",
                        "account","distribution_code","create_time")
                .eq(!ObjectUtils.isEmpty(account), "second_leader",account));

        PageInfo pageInfo = new PageInfo<>(list);
        //当前页
        int pageNum = pageInfo.getPageNum();
        //总页数
        int pages = pageInfo.getPages();
        //当前页展示的数量
        int pageSize = pageInfo.getSize();
        //查询总数
        long count = pageInfo.getTotal();
        PageResult<List<LsUser>> listPageResult = new PageResult<>(count, pages, pageSize, pageNum, list);

        return ResponseResult.responseResult(0, listPageResult);

    }

    @Override
    public ResponseResult<Object> updateStage(UpdateReq updateReq) throws ErrorException {
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LsUser lsUser = new LsUser();
        lsUser.setId(id);
        lsUser.setDisable(stage);

        int res = lsUserMapper.updateLsUser(lsUser);

        if(res > 0){
            return ResponseResult.responseResult(0,"解冻/冻结成功");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}


